package task.manager;

import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
import android.view.View;
import android.view.View.OnClickListener;

public class AddTaskScreen extends Activity {
	
	private Button taskSubmit;
	private Button cancelTask;
	private EditText nameBox;
	private EditText locationBox;
	private EditText addressBox;
	private EditText categoryBox;
	private DatePicker datePicker;
	private TimePicker timePicker;
	
	
	private Task newTask;
	private CategoryFacade categoryFacade;
	private TaskFacade taskFacade;
	private String activeCategory;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.addtaskscreen);
        
        categoryFacade = CategoryFacade.getInstance();
        taskFacade = TaskFacade.getInstance();
        System.out.println(taskFacade);
        System.out.println(taskFacade.getTaskList());
        
        taskSubmit = (Button)findViewById(R.id.task_submit);
        cancelTask = (Button)findViewById(R.id.cancel_task);
        nameBox = (EditText)findViewById(R.id.namebox);
        locationBox = (EditText)findViewById(R.id.locationbox);
        addressBox = (EditText)findViewById(R.id.addressbox);
        categoryBox = (EditText)findViewById(R.id.categorybox);
        datePicker = (DatePicker)findViewById(R.id.datePicker1);
        timePicker = (TimePicker)findViewById(R.id.timePicker1);
        
        
        activeCategory = categoryFacade.getActiveCategory();
        
        if(activeCategory!=null){
        	categoryBox.setText(activeCategory);
        }
        
        taskSubmit.setOnClickListener(new OnClickListener(){
        	public void onClick(View v) {
        		String taskName = nameBox.getText().toString();
        		String taskLocation = locationBox.getText().toString();
        		String taskAddress = addressBox.getText().toString();
        		String category = categoryBox.getText().toString();
        		
        		newTask = new Task(taskName,datePicker.getMonth(),datePicker.getDayOfMonth(),datePicker.getYear(),timePicker.getCurrentHour(),timePicker.getCurrentMinute());
        		System.out.println("New Task Title: " + newTask.getTitle());
        		if(!category.equals(activeCategory)) {
        			categoryFacade.setActiveCategory(category);
        			//categoryFacade.addNewCategory(new Category(category));
	        		/*try {
						taskFacade.loadTasks();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}*/
	        		//taskFacade.addTask(newTask);
	        		categoryFacade.addTaskToCategory(category,newTask);
	        		categoryFacade.setActiveCategory(activeCategory);
	        		/*try {
						taskFacade.loadTasks();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}*/
        		}
        		else {
        			System.out.println("Adding task: " + newTask.getTitle());
        			categoryFacade.addTaskToCategory(category,newTask);
        		}
        		
        		//Intent nextScreen = new Intent(getApplicationContext(), TaskViewActivity.class);
        		//startActivity(nextScreen);
        		//finish();
        		
        		Intent nextScreen = new Intent(getApplicationContext(), CategoryViewActivity.class);
        		startActivity(nextScreen);
        		finish();
        	}
        });
        
        cancelTask.setOnClickListener(new OnClickListener() {
        	public void onClick(View v) {
        		//Intent nextScreen = new Intent(getApplicationContext(), TaskViewActivity.class);
        		//startActivity(nextScreen);
        		//finish();
        		Intent nextScreen = new Intent(getApplicationContext(), CategoryViewActivity.class);
        		startActivity(nextScreen);
        		finish();
        	}
        });
	}
	
	@Override
	public void onPause(){
		super.onPause();
		
		try {
			taskFacade.saveTasks();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		
	}

}
